#include "logger.h"
#include <time.h>
#include <iostream>
// 获取日志的单例
Logger& Logger::GetInstance(){
    static Logger logger;
    return logger;
}
Logger::Logger()
{
     // 初始化信号量
    sem_init(&semaphore, 0, 0);
    std::cout<<"I'm in Logger::Logger()"<<std::endl;
    //启动专门的写日志线程
    std::thread writeLogTask([&](){
        for(;;){
            //获取当前的日期，然后取日志信息，写入相应的日志文件当中 a+
            time_t now=time(nullptr);
            tm *nowtm = localtime(&now);
            char file_name[128];
            sprintf(file_name,"%d-%d-%d-log.txt",nowtm->tm_year+1900,nowtm->tm_mon+1,nowtm->tm_mday);
            FILE *pf=fopen(file_name,"a+");
            if(pf==nullptr)
            {
                std::cout<<"logger file : "<<file_name<<" open error!"<<std::endl;
                exit(EXIT_FAILURE);
            }
            
            std::string msg=m_lckQue.Pop();
            char time_buf[128]={0};
            std::cout<<"I'm in Logger::Logger()::writeLogTask()"<<std::endl;
            sprintf(time_buf,"%d:%d:%d => [%s]" ,
                            nowtm->tm_hour,
                            nowtm->tm_min,
                            nowtm->tm_sec,
                            (m_loglevel==LogLevel::ERROR?"error":"info"));
            msg.insert(0,time_buf);
            msg.append("\n");
            fputs(msg.c_str(),pf);
            fclose(pf);
              // 等待信号量
            sem_post(&semaphore);
        }
    });
    //设置分离线程，守护线程
    writeLogTask.detach();
}
// 设置日志级别
void Logger::SetLogLevel(LogLevel level)
{
    m_loglevel=level;
    std::cout<<"I'm in Logger::SetLogLevel"<<std::endl;
}
// 写日志
void Logger::Log(std::string msg)
{
     std::cout<<"I'm in Logger::Log"<<std::endl;
    m_lckQue.Push(msg);
}